package io.github.yuazer.pokecommandpro.Listener;

import catserver.api.bukkit.ForgeEventV2;
import com.mc9y.nyeconomy.api.NyEconomyAPI;
import com.mc9y.pokestar.PokeStarAPI;
import com.pixelmonmod.pixelmon.api.events.BeatWildPixelmonEvent;
import com.pixelmonmod.pixelmon.api.events.CaptureEvent;
import com.pixelmonmod.pixelmon.api.pokemon.Element;
import com.pixelmonmod.pixelmon.api.pokemon.Pokemon;
import com.pixelmonmod.pixelmon.battles.controller.participants.PixelmonWrapper;
import io.github.yuazer.pokecommandpro.Main;
import io.github.yuazer.pokecommandpro.Utils.YamlUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.List;

public class PokemonEvent implements Listener {
    YamlUtils utils = new YamlUtils();
    PokeStarAPI pokeStarAPI = new PokeStarAPI();

    public boolean getChance(double chance) {
        boolean chanceBoolen = (chance >= 0.0D && chance <= 100.0D && Math.random() <= chance);
        return chanceBoolen;
    }

    @EventHandler
    public void onEvent(ForgeEventV2 event) {
        if (event.getForgeEvent() instanceof CaptureEvent.SuccessfulCapture) {
            CaptureEvent.SuccessfulCapture e = (CaptureEvent.SuccessfulCapture) event.getForgeEvent();
            Player player = Bukkit.getPlayer(e.player.func_110124_au());
            Pokemon pokemon = e.getPokemon().getPokemon();
            boolean pd1 = Main.getInstance().getConfig().getConfigurationSection("onCatch").getKeys(false).contains(pokemon.getSpecies().getName());
            boolean pd2 = getChance(utils.getConfigDouble("onCatch." + pokemon.getSpecies().getName() + ".chance"));
            boolean pd3 = (pokemon.getPokemonLevel() >= utils.getConfigInt("onCatch." + pokemon.getSpecies().getName() + ".level"));
            if (pd1 && pd2 && pd3) {
                List<String> commands = utils.getListString("onCatch." + pokemon.getSpecies().getName() + ".commands");
                for (String cmd : commands) {
                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", player.getName()));
                }
            }
            boolean pd4 = getChance(utils.getConfigDouble("onCatch.allPokemon.stars." + pokeStarAPI.getPokemonStar(pokemon.getSpecies().getName()) + ".chance"));
            if (pd4) {
                List<String> commands2 = utils.getListString
                        ("onCatch.allPokemon.stars." + com.mc9y.pokestar.Main.getPokeStarAPI().getPokemonStar(pokemon.getSpecies().getName()) + ".commands");
                for (String cmd : commands2) {
                    if (utils.getConfigBoolean("onBattle.allPokemon.ifDoubleTypes")) {
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", player.getName())
                                .replace("%number%", String.valueOf(getValue(pokemon)))
                                .replace("%type%", getElementsName(pokemon.getSpecies().getDefaultForm().getTypes())));
                    } else {
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", player.getName())
                                .replace("%number%", String.valueOf(getValue(pokemon)))
                                .replace("%type%", utils.getConfigMessage("type." + pokemon.getSpecies().getDefaultForm().getTypes().get(0).getName())));
                    }
                }
            }
            getNye(player, pokemon, "onCatch");
        }
        if (event.getForgeEvent() instanceof BeatWildPixelmonEvent) {
            BeatWildPixelmonEvent e = (BeatWildPixelmonEvent) event.getForgeEvent();
            Player player = Bukkit.getPlayer(e.player.func_110124_au());
            List<PixelmonWrapper> pokemons = e.wpp.controlledPokemon;
            for (PixelmonWrapper pokemon : pokemons) {
                String pokeName = pokemon.getSpecies().getName();
                boolean pd1 = Main.getInstance().getConfig().getConfigurationSection("onBattle").getKeys(false).contains(pokeName);
                boolean pd2 = getChance(utils.getConfigDouble("onBattle." + pokeName + ".chance"));
                boolean pd3 = pokemon.getPokemonLevelNum() >= utils.getConfigInt("onBattle." + pokeName + ".level");
                if (pd1 && pd2 && pd3) {
                    List<String> commands = utils.getListString("onBattle." + pokeName + ".commands");
                    for (String cmd : commands) {
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", player.getName())
                        );
                    }
                }
                boolean pd4 = getChance(utils.getConfigDouble("onBattle.allPokemon.stars." + pokeStarAPI.getPokemonStar(pokeName) + ".chance"));
                //boolean pd5 = (pokemon.getPokemonLevel().getPokemonLevel() >= utils.getConfigInt("onBattle.allPokemon.level"));
                if (pd4) {
                    //List<String> commands = utils.getListString("onBattle.allPokemon." + pokeStarAPI.getPokemonStar(pokeName) + ".stars.commands");
                    List<String> commands2 = utils.getListString
                            ("onBattle.allPokemon.stars." + com.mc9y.pokestar.Main.getPokeStarAPI().getPokemonStar(pokemon.getSpecies().getName()) + ".commands");
                    for (String cmd : commands2) {
                        if (utils.getConfigBoolean("onBattle.allPokemon.ifDoubleTypes")) {
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", player.getName())
                                    .replace("%number%", String.valueOf(getValue(pokemon.pokemon)))
                                    .replace("%type%", getElementsName(pokemon.type)));
                        } else {
                            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", player.getName())
                                    .replace("%number%", String.valueOf(getValue(pokemon.pokemon)))
                                    .replace("%type%", utils.getConfigMessage("type." + pokemon.type.get(0).getName())));
                        }
                    }
                }
                getNye(player, pokemon.pokemon, "onBattle");
            }
        }
    }

    private String getElementsName(List<Element> list) {
        if (list.size() > 1) {
            return utils.getConfigMessage("types." + list.get(0).getName()) + "和" + utils.getConfigMessage("types." + list.get(1).getName());

        }
        return utils.getConfigMessage("types." + list.get(0).getName());
    }
    /**
     * 获取NyEconomy货币
     * */
    private void getNye(Player player, Pokemon pokemon, String event) {
        if (utils.getConfigBoolean(event + ".allPokemon.ifDoubleTypes")) {
            for (Element element : pokemon.getSpecies().getDefaultForm().getTypes()) {
                NyEconomyAPI.getInstance().deposit(utils.getConfigMessage("types." + element.getName()), player.getName(), getDevide(getValue(pokemon)));
            }
        } else {
            NyEconomyAPI.getInstance().deposit(utils.getConfigMessage("types." + pokemon.getSpecies().getDefaultForm().getTypes().get(0).getName()), player.getName(), getValue(pokemon));
        }
    }
    /**
     * 当双属性币获取时，获取一半属性币
     * */
    private int getDevide(int value) {
        if (value % 2 != 0) {
            return (int) (value + 1 * 0.5);
        }
        return (int) (value * 0.5);
    }
    /**
     * 根据精灵等级获取对应属性币数量
     * */
    private int getValue(Pokemon pokemon) {
        int level = pokemon.getPokemonLevel();
        if (level % 10 != 0) {
            String a = String.valueOf(level);
            int newLevel = level - Integer.parseInt(a.substring(String.valueOf(level).length() - 1));
            return newLevel >= 10 ? (int) (newLevel * 0.1) : 0;
        }
        return (int) (level * 0.1);
    }
}
